// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   TrainServiceImp.java

package com.iphaeton.lms.business.organize;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.course.Course;
import com.iphaeton.lms.pojo.course.UserCourse;
import com.iphaeton.lms.pojo.organize.*;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.log4j.Logger;

// Referenced classes of package com.iphaeton.lms.business.organize:
//            ITrainService

public class TrainServiceImp extends PersistentServiceSupport
    implements ITrainService
{

    public TrainServiceImp()
    {
        cal = Calendar.getInstance();
        time = cal.getTime();
        df = new SimpleDateFormat("yyyy-MM-dd");
        currentTime = df.format(time);
    }

    public Train persistent(Train t)
        throws BusinessException
    {
        try
        {
            t = (Train)hbpservice.store(t);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6301\u4E45\u5316\u57F9\u8BAD\u73ED\u5931\u8D25\uFF01");
        }
        return t;
    }

    public Train store(Train t)
        throws BusinessException
    {
        try
        {
            t = (Train)hbpservice.store(t);
            hbpservice.commit();
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6301\u4E45\u5316\u57F9\u8BAD\u73ED\u5931\u8D25\uFF01");
        }
        return t;
    }

    public Pagination selectTrain(String userId, String row, String join, int state, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(t) from Train t where t.creater='" + userId + "' and t.deleted!=2 ";
        String hqlQueryStr = "from Train t where t.creater='" + userId + "' and t.deleted!=2 ";
        if(!"".equals(join))
        {
            selectHql = selectHql + " and t.joinAccess='" + join + "' and t.deleted!=2 ";
            hqlQueryStr = hqlQueryStr + " and t.joinAccess='" + join + "' and t.deleted!=2 ";
        }
        if(state == 1)
        {
            selectHql = selectHql + " and t.startDate>'" + currentTime + "'";
            hqlQueryStr = hqlQueryStr + " and t.startDate>'" + currentTime + "'";
        } else
        if(state == 2)
        {
            selectHql = selectHql + " and t.startDate<='" + currentTime + "' and t.endDate>='" + currentTime + "'";
            hqlQueryStr = hqlQueryStr + " and t.startDate<='" + currentTime + "' and t.endDate>='" + currentTime + "'";
        } else
        if(state == 3)
        {
            selectHql = selectHql + " and t.endDate<'" + currentTime + "'";
            hqlQueryStr = hqlQueryStr + " and t.endDate<'" + currentTime + "'";
        }
        if(row != null && !"".equals(row))
            hqlQueryStr = hqlQueryStr + " order by t." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by t.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find train!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List findPageList(Pagination pageInfo)
    {
        List list = null;
        try
        {
            list = getSpecifiedItems(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    public void assignCourse(String tId, String cId)
        throws BusinessException
    {
        String sql = "insert into t_train_course (train_id,course_id) values ('" + tId + "','" + cId + "')";
        try
        {
            jdbcpservice.execute(sql);
            Set trainUser = findTrainById(tId).getUser();
            User u;
            for(Iterator it = trainUser.iterator(); it.hasNext(); fullTrainCourse(tId, cId, u.getId()))
                u = (User)it.next();

        }
        catch(PersistenceException e)
        {
            removeTrain(tId);
            e.printStackTrace();
            throw new BusinessException("\u7ED9\u57F9\u8BAD\u73ED\u5206\u914D\u8BFE\u7A0B\u51FA\u9519!");
        }
    }

    public void assignStudent(String tId, String uId, int state)
        throws BusinessException
    {
        if(tId != null && uId != null && !"".equals(uId) && !"".equals(tId))
        {
            String sql = "insert into t_user_train (train_id,user_id,state) values ('" + tId + "','" + uId + "','" + state + "')";
            try
            {
                jdbcpservice.execute(sql);
                Set trainCourse = findTrainById(tId).getCourse();
                Course c;
                for(Iterator it = trainCourse.iterator(); it.hasNext(); fullTrainCourse(tId, c.getId(), uId))
                    c = (Course)it.next();

            }
            catch(PersistenceException e)
            {
                e.printStackTrace();
                throw new BusinessException("\u7ED9\u57F9\u8BAD\u73ED\u5206\u914D\u5B66\u5458\u51FA\u9519!");
            }
        }
    }

    private void fullTrainCourse(String train_id, String course_id, String user_id)
        throws BusinessException
    {
        if(!isUserTrainCourse(train_id, course_id, user_id))
        {
            UserCourse uc = new UserCourse();
            uc.setCourseId(course_id);
            uc.setUserId(user_id);
            uc.setTrainId(train_id);
            uc.setType(new Integer(2));
            uc.setState(new Integer(1));
            try
            {
                hbpservice.store(uc);
            }
            catch(PersistenceException e)
            {
                e.printStackTrace();
                throw new BusinessException("\u88C5\u8F7D\u57F9\u8BAD\u73ED\u8BFE\u7A0B\u65F6\u51FA\u9519!");
            }
        }
    }

    private boolean isUserTrainCourse(String train_id, String course_id, String user_id)
        throws BusinessException
    {
        boolean flag = false;
        String sql = "select * from t_user_course where user_id='" + user_id + "' and train_id='" + train_id + "' and course_id='" + course_id + "' and type=2";
        List list;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u9A8C\u8BC1\u7528\u6237\u57F9\u8BAD\u8BFE\u7A0B\u65F6\u51FA\u9519!");
        }
        if(list != null && list.size() > 0)
            flag = true;
        return flag;
    }

    public Train findTrainById(String id)
        throws BusinessException
    {
        Train train = null;
        try
        {
            train = (Train)hbpservice.get(id, com.iphaeton.lms.pojo.organize.Train.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u67E5\u627E\u57F9\u8BAD\u73ED\u5931\u8D25!");
        }
        return train;
    }

    public void removeTrain(String id)
        throws BusinessException
    {
        try
        {
            Train t = findTrainById(id);
            t.setDeleted(new Integer(2));
            hbpservice.store(t);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u903B\u8F91\u5220\u9664\u57F9\u8BAD\u73ED\u5931\u8D25!");
        }
    }

    public void deleteTrain(String id)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(id, com.iphaeton.lms.pojo.organize.Train.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u7269\u7406\u5220\u9664\u57F9\u8BAD\u73ED\u5931\u8D25!");
        }
    }

    public void assignGroup(String tId, String gId)
        throws BusinessException
    {
        String sql = "insert into t_group_train (train_id,group_id) values ('" + tId + "','" + gId + "')";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            removeTrain(tId);
            e.printStackTrace();
            throw new BusinessException("\u7ED9\u57F9\u8BAD\u73ED\u5206\u914D\u5C97\u4F4D\u804C\u52A1\u51FA\u9519!");
        }
    }

    public Pagination getTrainStudents(String trainId, int state, int pageSize)
        throws BusinessException
    {
        String selectSql = "select count(t.id) as count from t_user_train ut inner join t_user u on ut.train_id='" + trainId + "' and ut.user_id=u.id and ut.state='" + state + "' " + "inner join t_train t on t.id=ut.train_id " + "inner join t_organize o on u.organize_id=o.id";
        List list = null;
        try
        {
            list = jdbcpservice.query(selectSql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u57F9\u8BAD\u73ED\u5B66\u5458\u6570\u91CF\u5931\u8D25!");
        }
        logger.debug(new Integer(list.size()));
        logger.debug(((Map)list.get(0)).get("count"));
        int itemCount = list.size() <= 0 ? 0 : ((Integer)((Map)list.get(0)).get("count")).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        return pageInfo;
    }

    public List getTrainStudents(String trainId, String row, int state, int pageSize, int currentPage)
        throws BusinessException
    {
        List list = null;
        String sql = "select top " + pageSize + " u.user_id as userId,u.id as uid,u.name as uname,t.name as tname,ut.train_id as tid,o.url as oname" + " from t_user_train ut inner join t_user u on ut.train_id='" + trainId + "' and ut.user_id=u.id and ut.state='" + state + "' " + "inner join t_train t on t.id=ut.train_id " + "inner join t_organize o on u.organize_id=o.id " + " and u.user_id not in (select top " + pageSize * (currentPage - 1) + " u.user_id from t_user_train ut inner join t_user u on ut.train_id='" + trainId + "' and ut.user_id=u.id and ut.state='" + state + "' " + "inner join t_train t on t.id=ut.train_id " + "inner join t_organize o on u.organize_id=o.id) ";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u57F9\u8BAD\u73ED\u5B66\u5458\u5931\u8D25!");
        }
        return list;
    }

    public Pagination getTrainCourses(String trainId, int pageSize)
        throws BusinessException
    {
        String selectSql = "select count(c.id) as count from t_train_course tc  inner join t_course c on tc.train_id='" + trainId + "' and tc.course_id=c.id " + " inner join t_train t on t.id=tc.train_id ";
        List list = null;
        try
        {
            list = jdbcpservice.query(selectSql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u57F9\u8BAD\u73ED\u8BFE\u7A0B\u6570\u91CF\u5931\u8D25!");
        }
        logger.debug(new Integer(list.size()));
        logger.debug(((Map)list.get(0)).get("count"));
        int itemCount = list.size() <= 0 ? 0 : ((Integer)((Map)list.get(0)).get("count")).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        return pageInfo;
    }

    public List getTrainCourses(String trainId, String row, int pageSize, int currentPage)
        throws BusinessException
    {
        List list = null;
        if(row == null || "".equals(row))
            row = "cname";
        String sql = "select top " + pageSize + " c.id as cid,c.name as cname,c.hour as chour,cs.name as sname " + "from t_train_course tc " + "inner join t_course c on tc.train_id='" + trainId + "' and tc.course_id=c.id inner join t_course_sort cs on cs.id=c.sort_id" + " inner join t_train t on t.id=tc.train_id and c.id not in " + "(select top  " + pageSize * (currentPage - 1) + " c.id from t_train_course tc " + "inner join t_course c on tc.train_id='" + trainId + "' and tc.course_id=c.id " + "inner join t_train t on t.id=tc.train_id) order by " + row;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u57F9\u8BAD\u73ED\u8BFE\u7A0B\u5931\u8D25!");
        }
        return list;
    }

    public void confirmStudent(String trainId, String id)
        throws BusinessException
    {
        String sql = "update t_user_train set state=1 where user_id='" + id + "' and train_id='" + trainId + "'";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5BA1\u6279\u57F9\u8BAD\u73ED\u5B66\u5458\u5931\u8D25!");
        }
    }

    public void removeStudent(String user_id, String train_id)
        throws BusinessException
    {
        String sql = "delete from t_user_train where user_id='" + user_id + "' and train_id='" + train_id + "'";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u57F9\u8BAD\u73ED\u5B66\u5458\u5931\u8D25!");
        }
    }

    public void removeCourse(String course_id, String train_id)
        throws BusinessException
    {
        String sql = "delete from t_train_course where course_id='" + course_id + "' and train_id='" + train_id + "'";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u57F9\u8BAD\u73ED\u8BFE\u7A0B\u5931\u8D25!");
        }
    }

    public List currentTrain(User u, String row, int state)
        throws BusinessException
    {
        String selectSql1 = "";
        String selectSql2 = "";
        String selectSql3 = "";
        String selectSql4 = "";
        selectSql1 = selectSql1 + "select t.id as tid,t.name as tname,t.sort as sort,o.url as oname,t.start_date as tstart,t.end_date as tend,t.teacher as teacher,t.type as type,t.comment as comment,t.creater as creater from t_train t inner join t_organize o on t.deleted!=2 and t.organize_id=o.id and '" + u.getOrganize().getUrl() + "' like o.url" + "+'%'" + " and t.start_date>'" + currentTime + "' and t.join_access='apply' and t.sort='normal' ";
        selectSql2 = selectSql2 + " union (select t.id as tid,t.name as tname,t.sort as sort,o.url as oname,t.start_date as tstart,t.end_date as tend,t.teacher as teacher,t.type as type,t.comment as comment,t.creater as creater from t_train t inner join t_group g on t.deleted!=2 and t.start_date>'" + currentTime + "' and t.join_access='apply' " + "inner join t_organize o on g.organize_id=o.id and '" + u.getOrganize().getUrl() + "' like o.url" + "+'%'" + "inner join t_group_train gt on t.id=gt.train_id " + "and gt.group_id=g.id and t.sort!='normal' ";
        if(state == 1)
            selectSql3 = selectSql3 + " inner join t_user_train ut on ut.user_id='" + u.getId() + "' and ut.train_id=t.id and ut.state=2";
        else
        if(state == 2)
            selectSql3 = selectSql3 + " and t.id not in (select ut.train_id from t_user_train ut where ut.user_id='" + u.getId() + "') ";
        if(row != null || "".equals(row))
            selectSql4 = selectSql4 + ") order by " + row;
        else
            selectSql4 = selectSql4 + ") order by tname";
        List list = null;
        try
        {
            logger.info("sql:" + selectSql1 + selectSql3 + selectSql2 + selectSql3 + selectSql4);
            list = jdbcpservice.query(selectSql1 + selectSql3 + selectSql2 + selectSql3 + selectSql4);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find train!");
        }
        return list;
    }

    public List myTrain(String user_id, String row, int state)
        throws BusinessException
    {
        List list = null;
        String sql = "select t.id as tid,t.name as tname,t.sort as sort,o.url as oname,t.start_date as tstart,t.end_date as tend,t.teacher as teacher,t.type as type,t.comment as comment,t.creater as creater from t_train t  inner join t_organize o on t.deleted!=2 and t.organize_id=o.id and t.end_date>='" + currentTime + "' " + " inner join t_user_train ut on ut.train_id=t.id and ut.user_id='" + user_id + "'";
        if(state == 1 || state == 2)
            sql = sql + " and ut.state='" + state + "'";
        if(row != null && !"".equals(row))
            sql = sql + " order by " + row;
        else
            sql = sql + " order by tname";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u627E\u7528\u6237\u5F53\u524D\u57F9\u8BAD\u73ED\u5931\u8D25!");
        }
        return list;
    }

    public Pagination myManageTrain(String userId, String orgUrl, String row, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(t) from Train t where t.deleted!=2 and (t.creater='" + userId + "' or t.organize.url like '" + orgUrl + "%' ) ";
        String hqlQueryStr = "from Train t where t.deleted!=2 and (t.creater='" + userId + "' or t.organize.url like '" + orgUrl + "%' ) ";
        if(row != null && !"".equals(row))
            hqlQueryStr = hqlQueryStr + " order by t." + row;
        else
            hqlQueryStr = hqlQueryStr + " order by t.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find train!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List findAdminTrain(String user_id)
        throws BusinessException
    {
        List list = null;
        String hql = "from Train t where t.creater='" + user_id + "' and t.deleted!=2 ";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7528\u6237\u521B\u5EFA\u7684\u57F9\u8BAD\u73ED\u5217\u8868\u5931\u8D25!");
        }
        return list;
    }

    public List getTrainReport(String id, String course_id, String user_id, String row)
        throws BusinessException
    {
        List list = null;
        String sql = "select c.name as cname, c.id as cid, c.hour as chour, u.user_id as user_id, u.name as uname, o.url as oname, uc.period as period from t_user_train ut inner join t_user_course uc on ut.train_id = '" + id + "' and uc.train_id = ut.train_id ";
        if(course_id != null && !"".equals(course_id))
            sql = sql + " and uc.course_id='" + course_id + "' ";
        if(user_id != null && !"".equals(user_id))
            sql = sql + " and uc.user_id='" + user_id + "' ";
        sql = sql + "inner join t_course c on uc.course_id = c.id inner join t_user u on u.id = uc.user_id inner join t_organize o on u.organize_id = o.id group by c.name,c.id, c.hour, u.user_id, u.name, o.url, uc.period";
        if(row != null && !"".equals(row))
            sql = sql + " order by c." + row;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u57F9\u8BAD\u62A5\u8868\u51FA\u9519!");
        }
        return list;
    }

    public List getTrainUserCourse(String user_id, String train_id)
        throws BusinessException
    {
        String hql = "from UserCourse uc where uc.trainId='" + train_id + "' and uc.userId='" + user_id + "' and uc.type=2 and uc.state=1";
        List list;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u7528\u6237\u6307\u5B9A\u57F9\u8BAD\u73ED\u57F9\u8BAD\u8BFE\u7A0B\u65F6\u51FA\u9519!");
        }
        return list;
    }

    private static Logger logger;
    Calendar cal;
    Date time;
    DateFormat df;
    String currentTime;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.organize.TrainServiceImp.class.getName());
    }
}
